package com.wind;

import android.Manifest;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.ListView;
import android.widget.Toast;

import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;
import kr.co.namee.permissiongen.PermissionSuccess;


public class MainActivity extends BaseActivity<BuyBookPresenter> implements IBuyBookView {

    ListView listView;

    private BuyBookAdapter mAdapter;

    BuyBookPresenter buyBookPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    BuyBookPresenter initPresent() {
        buyBookPresenter = new BuyBookPresenter();
        buyBookPresenter.attachView(this);
        return buyBookPresenter;
    }

    @Override
    int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    void onPrepare() {
        listView = findViewById(R.id.listview);
        mAdapter = new BuyBookAdapter(this, ((BuyBookPresenter)presenter).getAdapterData());
        listView.setAdapter(mAdapter);
        presenter.initData();
        PermissionGen.with(MainActivity.this).addRequestCode(100).permissions(
                Manifest.permission.CAMERA).request();
    }

    @Override
    public void refreshAdapter() {
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onEmpty() {
        listView.setEmptyView(null);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        buyBookPresenter.detachView();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }


    @PermissionSuccess(requestCode = 100)
    public void doSomething(){
        Toast.makeText(this, "Contact permission is granted", Toast.LENGTH_SHORT).show();
    }


    @PermissionFail(requestCode = 100)
    public void doFailSomething(){
        Toast.makeText(this, "Contact permission is not granted", Toast.LENGTH_SHORT).show();
    }

}
